<template>
  <div class="">
    <div class="big">
      <el-input
        v-model="inputValue"
        placeholder="输入用户编码查询"
        class="top"
      />
      <el-button type="primary" class="chaxun" @click="yhcx">查询</el-button>
      <el-button>新增</el-button>
    </div>
    <el-table :data="userList" style="width: 100%">
      <el-table-column fixed prop="id" label="用户编码" width="150" />
      <el-table-column prop="name" label="用户姓名" width="120" />
      <!-- <el-table-column prop="state" label="性别" width="120" /> -->
      <el-table-column prop="dept" label="所属部门" width="120" />
      <el-table-column prop="address" label="地址" width="600" />
      <el-table-column prop="induction" label="入职日期" width="120" />
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleClick"
            >编辑</el-button
          >
          <el-button link type="primary" size="small" @click="scyh(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev,pager,next"
      :total="total"
      :page-size="sizes"
      @current-change="handChange"
    />
  </div>
</template>

<script>
import { userlist, userinfo, userdelete } from "@/apis/userlist";
export default {
  data() {
    return {
      userList: [],
      userInfo: [],
      total: 0,
      pages: 1,
      sizes: 20,
      index: 0,
      inputValue: "",
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    // 获取用户列表信息
    async getUser(page = this.pages, size = this.sizes, name, state) {
      try {
        const res = await userlist({ page, size, name, state });
        if (res.code === 200) {
          this.userList = res.data.rows;
          this.total = res.data.count;
        }
      } catch (err) {
        console.log(err);
      }
    },
    handChange(e){
      this.pages=e
      this.getUser()
    },
    // 查询用户
    async yhcx() {
      try {
        const res = await userinfo({ id: this.inputValue });
        if (res.code === 200) {
          this.userList = [res.data]
        }
      } catch (err) {
        console.log(err);
      }
    },
    // 删除用户
    async scyh(index) {
      try {
        const res = await userdelete(this.userList[index].id);
        if (res.code === 200) {
          this.userList.splice(index, 1)
        }
      } catch (err) {
        console.log(err);
      }
    },
    handleClick() {
      this.$router.push("/user/edit");
    },
  },
};
</script>

<style lang="less" scoped>
.top {
  width: 200px;
}
.chaxun {
  margin-left: 12px;
}
.big {
  width: 350px;
  float: right;
  margin-right: 150px;
}
.el-pagination {
  position: absolute;
  left: 42%;
}
</style>